<template lang="html">
  <transition name="move">
    <div class="food" v-show="showFlay" ref="food">
      <div class="food-content">
        <div class="img-header">
          <img :src="foods.image" alt="">
          <div class="back" @click="hide">
            <i class="icon-arrow_lift"></i>
          </div>
        </div>
        <div class="content">
          <h1 class="name">{{foods.name}}</h1>
          <div class="detail">
            <span class="sell-count">月售{{foods.sellCount}}份</span>
            <span class="rating">好评率{{foods.rating}}%</span>
          </div>
          <div class="price">
            <span class="new">￥{{foods.price}}</span>
            <span class="old" v-show="foods.oldPrice">￥{{foods.oldPrice}}</span>
          </div>
          <div class="cartcontrol-wrapper">
            <cartcontrol :food="foods" @cart-add="_drop"></cartcontrol>
          </div>
          <div class="buy" v-show="!foods.count || foods.count === 0" @click.stop.prevent="addFirst">加入购物车</div>
        </div>
        <split v-show="foods.info"></split>
        <div class="info" v-show="foods.info">
          <h1 class="title">商品介绍</h1>
          <p class="text">{{foods.info}}</p>
        </div>
        <split></split>
        <div class="rating">
          <h1 class="title">商品评价</h1>
          <ratingselect @select-type="_selectType" @only-content="_onlyContent" :onlyContent="onlyContent" :ratings="foods.ratings" :desc="desc" :selectType="selectType"></ratingselect>
          <div class="ratings-wrapper">
            <ul v-show="foods.ratings && foods.ratings.length">
              <li v-show="needShow(rating.rateType, rating.text)" v-for="rating in foods.ratings" :key="rating.index" class="ratings-item border-1px">
                <div class="user">
                  <span class="name">{{rating.username}}</span>
                  <img :src="rating.avatar" width="12" height="12" alt="">
                </div>
                <div class="time">
                  {{rating.rateTime | formatDate}}
                </div>
                <p class="text">
                  <span :class="{'icon-thumb_up':rating.rateType === 0,'icon-thumb_down': rating.rateType === 1}"></span>{{rating.text}}
                </p>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script type="text/ecmascript0-6">
import Vue from 'vue';
import BScroll from 'better-scroll';
import split from '@/components/split/split.vue';
import ratingselect from '@/components/ratingselect/ratingselect.vue';
import cartcontrol from '@/components/cartcontrol/cartcontrol.vue';
import {formatDate} from '@/common/js/data.js';

const ALL = 2;

export default {
  name: 'food',
  props: {
    foods: {
      type: Object
    }
  },
  data() {
    return {
      showFlay: false,
      selectType: ALL,
      desc: {
        all: '全部',
        positive: '推荐',
        negative: '吐槽'
      },
      onlyContent: true
    };
  },
  components: {
    split,
    ratingselect,
    cartcontrol
  },
  methods: {
    show() {
      this.showFlay = true;
      this.selectType = ALL;
      this.onlyContent = true;
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(this.$refs.food, {
            click: true
          });
        } else {
          this.scroll.refresh();
        }
      });
    },
    hide() {
      this.showFlay = false;
    },
    _selectType(type) {
      this.selectType = type;
      this.scroll.refresh();
    },
    _onlyContent(type) {
      this.onlyContent = type;
      this.scroll.refresh();
    },
    _drop(target) {
      this.$emit('cart-add', target);
    },
    needShow(type, text) {
      if (this.onlyContent && !text) {
        return false;
      }

      if (this.selectType === ALL) {
        return true;
      } else {
        return this.selectType === type;
      }
    },
    addFirst(event) {
      this.$emit('cart-add', event.target);
      Vue.set(this.foods, 'count', 1);
    }
  },
  filters: {
    formatDate(time) {
      let date = new Date();
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus">
@import '../../common/stylus/mixin.styl';

  .food
    position: fixed
    top: 0
    left: 0
    bottom: 48px
    width: 100%
    overflow: hidden
    z-index: 20
    background: #fff
    &.move-enter-active, &.move-leave-active
      transition: all .2s linear
      transform: translate3d(0, 0, 0)
    &.move-enter, &.move-leave-active
      transform: translate3d(100%, 0, 0)
    .img-header
      position: relative
      top: 0
      left: 0
      width: 100%
      height: 0
      padding-top: 100%
      img
        position: absolute
        top: 0
        left: 0
        width: 100%
        height: 100%
      .back
        position: absolute
        top: 10px
        left: 0
        .icon-arrow_lift
          display: block
          padding: 10px
          font-size: 20px
          color: #fff
    .content
      position: relative
      padding: 18px
      .name
        line-height: 14px
        font-size: 14px
        font-weight: 700
        color: rgb(7, 17, 27)
      .detail
        margin-top: 8px
        font-size: 0
        .sell-count, .rating
          line-height: 10px
          font-size: 10px
          color: rgb(147, 153, 159)
        .sell-count
          margin-right: 12px
      .price
        margin-top: 18px
        font-size: 0
        .new
          margin-right: 12px
          line-height: 24px
          font-size: 14px
          font-weight: 700
          color: rgb(240, 20, 20)
        .old
          line-height: 24px
          font-size: 10px
          font-weight: 700
          color: rgb(147, 153, 159)
      .cartcontrol-wrapper, .buy
        position: absolute
        right: 18px
        bottom: 18px
      .buy
        width: 74px
        padding: 6px 0
        line-height: 12px
        border-radius: 12px
        text-align: center
        z-inde: 10
        font-size: 10px
        color: #fff
        background: rgb(0, 160, 220)
    .info
      padding: 18px
      .title
        margin-bottom: 6px
        line-height: 14px
        font-size: 14px
        font-weight: 700
        color: rgb(7, 17, 27)
      .text
        padding: 0 8px
        line-height: 24px
        font-size: 14px
        color: rgb(77, 85, 93)
    .rating
      margin-top: 18px
      .title
        margin-left: 18px
        line-height: 14px
        font-size: 14px
        font-weight: 700
        color: rgb(7, 17, 27)
      .ratings-wrapper
        padding: 0 18px
        .ratings-item
          position: relative
          padding: 16px 0
          border-1px(rgba(7, 17, 27, .1))
          .user
            position: absolute
            top: 16px
            right: 0
            font-size: 0
            .name
             vertical-align: top
             margin-right: 6px
             line-height: 12px
             font-size: 10px
             color: rgb(147, 153, 159)
            img
              vertical-align: top
              border-radius: 50%
          .time
            line-height: 12px
            font-size: 10px
            color: rgb(147, 153, 159)
          .text
            margin-top: 6px
            line-height: 16px
            font-size: 12px
            color: rgb(7, 17, 27)
            .icon-thumb_up, .icon-thumb_down
              display: inline-block
              margin-right: 4px
              line-height: 16px
              font-size: 12px
            .icon-thumb_up
              color: rgb(0, 160, 220)
            .icon-thumb_down
              color: rgb(147, 153, 159)
</style>
